﻿<%@ WebHandler Language="C#" Class="ScrapReport" %>

using System;
using System.IO;
using System.Web;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Net;
using ReportBusiness;

public class ScrapReport : IHttpHandler
{
    uMESReportOracleDAL dal = new uMESReportOracleDAL();
    UserImage u = new UserImage();
    public bool IsReusable { get { return false; } }
    public void ProcessRequest(HttpContext context)
    {
        string requestjson = "";
        using (StreamReader reader = new StreamReader(context.Request.InputStream)) { requestjson = reader.ReadToEnd(); }
        JObject jObject = JsonConvert.DeserializeObject(requestjson) as JObject;
        if (jObject["type"] != null)
        {
            try
            {
                Result r = new Result();
                context.Response.ContentType = "text/json";
                string type = jObject["type"].ToString();
                switch (type)
                {
                    case "getData":
                        r = getData(jObject);//查询
                        context.Response.Write(JsonConvert.SerializeObject(r));
                        break;
                    case "exportExcel":
                        exportExcel(jObject);//导出
                        break;
                    default:
                        break;
                }
            }
            catch (Exception e)
            {
                Result r = new Result();
                r.data = null;
                r.meta = new Result.Meta(500, e.Message);
                r.total = 0;
                context.Response.ContentType = "text/json";
                context.Response.Write(JsonConvert.SerializeObject(r));
            }
        }
    }

    //查询方法
    Result getData(JObject jObject)
    {
        Result r = new Result();
        string sql = @"
SELECT cci.familylistnames productfamily,cci.productname,cci.containername,
       CASE WHEN cci.containertype = 3 THEN c.containername ELSE '' END childcontainername,--子批次号
       wss.workflowstepname,abs(cr.qty) scrapqty,
       ep.fullname reportPerson,cr.createdate checkvalidatedate,lrs.lossreasonname,ce.fullname checkPerson,cci.fromworkcentername workcentername,
       cci.mfgordername,csc.zzrz,
       DECODE(csc.zzfplx,'01','加工原因报废','02','材质原因报废','03','试验报废','04','节余报废','05','外委加工报废','06','设计原因报废','07','工艺原因报废','08','设备原因报废','09','修理机排故机更换报废','其他报废') zzfplx,
       ep.employeename worker,
       ce.employeename checker,
       f.factoryname,f2.factoryname tofactoryname
FROM CheckQtyChangeRecord cr
LEFT JOIN container c ON c.containerid = cr.containerid
LEFT JOIN containercurrentinfo cci ON cci.containerid = NVL(c.parentcontainerid,cr.containerid)
LEFT JOIN Workflowstep wss ON wss.workflowstepid = cr.workflowstepid
LEFT JOIN containerstepworkreport cwr ON cwr.containerstepworkreportid = cr.containerstepworkreportid AND cwr.isuse = 0
LEFT JOIN employee ep ON ep.employeeid = cwr.workerid
LEFT JOIN lossreason lrs ON lrs.lossreasonid = cr.reasonid
LEFT JOIN employee ce ON ce.employeeid = cr.employeeid --检验员
LEFT JOIN containerstepscrapinfo csc ON csc.containerid = cci.containerid
LEFT JOIN factory f ON f.factoryid = cci.originalfactoryid
LEFT JOIN Sessionvalues sv ON sv.employeeid = cr.employeeid
LEFT JOIN factory f2 ON f2.factoryid = sv.factoryid
WHERE cr.isuse = 0 AND cr.changeqtytype = 2 AND wss.workflowstepname IS NOT NULL ";
        //主制分厂
        if (jObject["factoryid"] != null && jObject["factoryid"].ToString() != "")
        {
            sql += string.Format(" AND cci.originalfactoryid = '{0}'", jObject["factoryid"].ToString());
        }

        //承制分厂
        if (jObject["tofactoryid"] != null && jObject["tofactoryid"].ToString() != "")
        {
            sql += string.Format(" AND f2.factoryid = '{0}'", jObject["tofactoryid"].ToString());
        }

        // 工区
        if (jObject["workcenterid"] != null && jObject["workcenterid"].ToString() != "")
        {
            sql += string.Format(" AND cci.fromworkcenterid = '{0}'", jObject["workcenterid"].ToString());
        }

        // 操作工
        if (jObject["workerid"] != null && jObject["workerid"].ToString() != "")
        {
            sql += string.Format("  AND ep.employeeid = '{0}'", jObject["workerid"].ToString());
        }

        // 检验员
        if (jObject["checkerid"] != null && jObject["checkerid"].ToString() != "")
        {
            sql += string.Format("  AND ce.employeeid = '{0}'", jObject["checkerid"].ToString());
        }

        // 机型
        if (jObject["productfamilyname"] != null && jObject["productfamilyname"].ToString() != "")
        {
            sql += string.Format(" AND cci.familylistnames LIKE '%{0}%'", jObject["productfamilyname"].ToString());
        }

        // 零件图号
        if (jObject["productname"] != null && jObject["productname"].ToString() != "")
        {
            sql += string.Format(" AND cci.productname = '{0}'", jObject["productname"].ToString());
        }

        // 订单号
        if (jObject["mfgordername"] != null && jObject["mfgordername"].ToString() != "")
        {
            sql += string.Format(" AND cci.mfgordername = '{0}'", jObject["mfgordername"].ToString());
        }

        // 批次号
        if (jObject["togetherflag"] != null && jObject["togetherflag"].ToString() != "")
        {
            sql += string.Format(" AND cci.togetherflag LIKE '%{0}%'", jObject["togetherflag"].ToString());
        }

        // 流水卡号
        if (jObject["containername"] != null && jObject["containername"].ToString() != "")
        {
            sql += string.Format(" AND cci.containername = '{0}'", jObject["containername"].ToString());
        }

        // 报废时间
        if (jObject["createDate"] != null && jObject["createDate"].ToString() != "")
        {
            JArray arr = JArray.Parse(jObject["createDate"].ToString());
            if (arr.Count >= 2)
            {
                sql += string.Format(" AND TRUNC(cr.createdate) >= to_date('{0}','yyyy-MM-dd') ", arr[0].ToString());
                sql += string.Format(" AND TRUNC(cr.createdate) <= to_date('{0}','yyyy-MM-dd') ", arr[1].ToString());
            }
        }

        uMESPagingDataDTO dto = dal.GetPagingDataIns(sql, (int)jObject["currentPage"], (int)jObject["pageSize"]);
        DataTable dt = dto.DBTable;
        foreach (DataColumn col in dt.Columns)
        {
            col.ColumnName = col.ColumnName.ToLower();
        }
        r.data = dt;
        r.total = Convert.ToInt32(dto.RowCount);
        r.meta = new Result.Meta(200, "请求成功");
        return r;
    }

    //导出
    void exportExcel(JObject jObject)
    {
        string sql = @"
SELECT cci.familylistnames,cci.mfgordername,f.factoryname,cci.fromworkcentername,
       cci.productname,cci.containername,
       CASE WHEN cci.containertype = 3 THEN c.containername ELSE '' END childcontainername,--子批次号
       wss.workflowstepname,
       ep.fullname reportPerson,
       ce.fullname checkPerson,cr.createdate,abs(cr.qty) scrapqty,lrs.lossreasonname,
       csc.zzrz,
       DECODE(csc.zzfplx,'01','加工原因报废','02','材质原因报废','03','试验报废','04','节余报废','05','外委加工报废','06','设计原因报废','07','工艺原因报废','08','设备原因报废','09','修理机排故机更换报废','其他报废') zzfplx
FROM CheckQtyChangeRecord cr
LEFT JOIN container c ON c.containerid = cr.containerid
LEFT JOIN containercurrentinfo cci ON cci.containerid = NVL(c.parentcontainerid,cr.containerid)
LEFT JOIN Workflowstep wss ON wss.workflowstepid = cr.workflowstepid
LEFT JOIN containerstepworkreport cwr ON cwr.containerstepworkreportid = cr.containerstepworkreportid AND cwr.isuse = 0
LEFT JOIN employee ep ON ep.employeeid = cwr.workerid
LEFT JOIN lossreason lrs ON lrs.lossreasonid = cr.reasonid
LEFT JOIN employee ce ON ce.employeeid = cr.employeeid
LEFT JOIN containerstepscrapinfo csc ON csc.containerid = cci.containerid
LEFT JOIN factory f ON f.factoryid = cci.originalfactoryid
WHERE cr.isuse = 0 AND cr.changeqtytype = 2 AND wss.workflowstepname IS NOT NULL ";
        // 分厂
        if (jObject["factoryid"] != null && jObject["factoryid"].ToString() != "")
        {
            sql += string.Format(" AND cci.originalfactoryid = '{0}'", jObject["factoryid"].ToString());
        }

        // 工区
        if (jObject["workcenterid"] != null && jObject["workcenterid"].ToString() != "")
        {
            sql += string.Format(" AND cci.fromworkcenterid = '{0}'", jObject["workcenterid"].ToString());
        }

        // 操作工
        if (jObject["workerid"] != null && jObject["workerid"].ToString() != "")
        {
            sql += string.Format("  AND ep.employeeid = '{0}'", jObject["workerid"].ToString());
        }

        // 检验员
        if (jObject["checkerid"] != null && jObject["checkerid"].ToString() != "")
        {
            sql += string.Format("  AND ce.employeeid = '{0}'", jObject["checkerid"].ToString());
        }

        // 机型
        if (jObject["productfamilyname"] != null && jObject["productfamilyname"].ToString() != "")
        {
            sql += string.Format(" AND cci.familylistnames LIKE '%{0}%'", jObject["productfamilyname"].ToString());
        }

        // 零件图号
        if (jObject["productname"] != null && jObject["productname"].ToString() != "")
        {
            sql += string.Format(" AND cci.productname = '{0}'", jObject["productname"].ToString());
        }

        // 订单号
        if (jObject["mfgordername"] != null && jObject["mfgordername"].ToString() != "")
        {
            sql += string.Format(" AND cci.mfgordername = '{0}'", jObject["mfgordername"].ToString());
        }

        // 批次号
        if (jObject["togetherflag"] != null && jObject["togetherflag"].ToString() != "")
        {
            sql += string.Format(" AND cci.togetherflag LIKE '%{0}%'", jObject["togetherflag"].ToString());
        }

        // 流水卡号
        if (jObject["containername"] != null && jObject["containername"].ToString() != "")
        {
            sql += string.Format(" AND cci.containername = '{0}'", jObject["containername"].ToString());
        }

        // 报废时间
        if (jObject["createDate"] != null && jObject["createDate"].ToString() != "")
        {
            JArray arr = JArray.Parse(jObject["createDate"].ToString());
            if (arr.Count >= 2)
            {
                sql += string.Format(" AND TRUNC(cr.createdate) >= to_date('{0}','yyyy-MM-dd') ", arr[0].ToString());
                sql += string.Format(" AND TRUNC(cr.createdate) <= to_date('{0}','yyyy-MM-dd') ", arr[1].ToString());
            }
        }

        DataTable dt = dal.GetDataTable(sql);
        dt.Columns["familylistnames"].Caption = "机型";
        dt.Columns["productname"].Caption = "图号";
        dt.Columns["mfgordername"].Caption = "订单号";
        dt.Columns["containername"].Caption = "流水卡号";
        dt.Columns["factoryname"].Caption = "分厂";
        dt.Columns["fromworkcentername"].Caption = "工区";
        dt.Columns["workflowstepname"].Caption = "工序";
        dt.Columns["scrapqty"].Caption = "报废数";
        dt.Columns["reportPerson"].Caption = "操作工";
        dt.Columns["createdate"].Caption = "报废时间";
        dt.Columns["lossreasonname"].Caption = "报废原因";
        dt.Columns["checkPerson"].Caption = "检验员";
        dt.Columns["zzrz"].Caption = "责任者";
        dt.Columns["zzfplx"].Caption = "废品类型";
        dt.Columns["childcontainername"].Caption = "子批次号";
        ExportHelper.ExportExcel(dt, "报废品查询报表");
    }
}